package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.CategoryActivity;
import flc.ast.activity.DetailActivity;
import flc.ast.activity.GuideActivity;
import flc.ast.activity.GuideDetailActivity;
import flc.ast.activity.NpcActivity;
import flc.ast.activity.StrategyDetailActivity;
import flc.ast.adapter.MyBannerAdapter;
import flc.ast.adapter.MyHomeBannerAdapter;
import flc.ast.adapter.NpcTabAdapter;
import flc.ast.databinding.FragmentEquipmentBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkTagBean;
import stark.common.bean.StkTagResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link EquipmentFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class EquipmentFragment extends BaseNoModelFragment<FragmentEquipmentBinding> {

    private int selPosition;
    private NpcTabAdapter npcTabAdapter;
    private List<StkTagResBean> listCategory = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.fragment_equipment;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivCategory1.setOnClickListener(this);
        mDataBinding.ivCategory2.setOnClickListener(this);
        mDataBinding.ivCategory3.setOnClickListener(this);
        mDataBinding.ivCategory4.setOnClickListener(this);
        mDataBinding.ivCategory5.setOnClickListener(this);
        mDataBinding.ivCategory6.setOnClickListener(this);
        mDataBinding.ivCategory7.setOnClickListener(this);
        mDataBinding.ivCategory8.setOnClickListener(this);
        mDataBinding.ivCategory9.setOnClickListener(this);
        mDataBinding.ivPrevious.setOnClickListener(this);
        mDataBinding.ivNext.setOnClickListener(this);
        mDataBinding.ivNpcMore.setOnClickListener(this);

        mDataBinding.rvNpcList.setLayoutManager(new LinearLayoutManager(mContext));
        npcTabAdapter = new NpcTabAdapter();
        mDataBinding.rvNpcList.setAdapter(npcTabAdapter);
        npcTabAdapter.addChildClickViewIds(R.id.tvNpcTabItemDetail);
        npcTabAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPrevious://上一页
                if (selPosition == 0) {
                    selPosition = 2;
                } else {
                    selPosition--;
                }
                clearSelection();
                break;
            case R.id.ivNext://下一页
                if (selPosition == 2) {
                    selPosition = 0;
                } else {
                    selPosition++;
                }
                clearSelection();
                break;
            case R.id.ivNpcMore://npc支线查看更多
                startActivity(NpcActivity.class);
                break;
            case R.id.ivCategory1:
                goToList(0);
                break;
            case R.id.ivCategory2:
                goToList(1);
                break;
            case R.id.ivCategory3:
                goToList(2);
                break;
            case R.id.ivCategory4:
                goToList(3);
                break;
            case R.id.ivCategory5:
                goToList(4);
                break;
            case R.id.ivCategory6:
                goToList(5);
                break;
            case R.id.ivCategory7:
                goToList(6);
                break;
            case R.id.ivCategory8:
                goToList(7);
                break;
            case R.id.ivCategory9:
                goToList(8);
                break;
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        GuideDetailActivity.stkResBean = npcTabAdapter.getItem(position);
        startActivity(GuideDetailActivity.class);
    }

    @Override
    protected void initData() {
        getCategoryData();
        getNpcData();
        getStrategyData();
    }

    private void getCategoryData() {
        StkResApi.getChildTagList(this, AppConfig.CATEGORY_DATA, StkResApi.createParamMap(0, 9), false, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        listCategory = result;
                    }
                }
            }
        });
    }

    private void getNpcData() {
        StkResApi.getTagResourceList(this, AppConfig.NPC_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        npcTabAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getStrategyData() {
        StkResApi.getTagResourceList(this, AppConfig.STRATEGY_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        MyBannerAdapter bannerAdapter = new MyBannerAdapter(result);
                        mDataBinding.banner.setAdapter(bannerAdapter, true);//false不无限循环
                        mDataBinding.banner.setBannerGalleryEffect(80, 80, 20, 1.0f);
                        mDataBinding.banner.isAutoLoop(false);//false不自动轮播
                        //点击监听
                        mDataBinding.banner.setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(Object data, int position) {
                                StrategyDetailActivity.stkResBean = result.get(position);
                                startActivity(StrategyDetailActivity.class);
                            }
                        });
                    }
                }
            }
        });
    }

    private void goToList(int position) {
        CategoryActivity.stkTagResBean = listCategory.get(position);
        startActivity(CategoryActivity.class);
    }

    private void clearSelection() {
        mDataBinding.llCategory1.setVisibility(View.GONE);
        mDataBinding.llCategory2.setVisibility(View.GONE);
        mDataBinding.llCategory3.setVisibility(View.GONE);
        if (selPosition == 0) {
            mDataBinding.llCategory1.setVisibility(View.VISIBLE);
        } else if (selPosition == 1) {
            mDataBinding.llCategory2.setVisibility(View.VISIBLE);
        } else if (selPosition == 2) {
            mDataBinding.llCategory3.setVisibility(View.VISIBLE);
        }
    }
}